package com.mediacloud.app.newsmodule.fragment.ugc.view

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.fragment.ugc.LiteAvHotDetailActivity
import com.mediacloud.app.newsmodule.fragment.ugc.mod.CmsCatalog

class LiteAvHotView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : LinearLayout(context, attrs, defStyleAttr) {
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context) : this(context, null)

    var recyclerView: RecyclerView
    var adapter: LiteAvHotAdapter

    init {
        orientation = HORIZONTAL
        gravity = Gravity.CENTER_VERTICAL
        val ico = ImageView(context)
        ico.setImageResource(R.drawable.lite_av_hot_ico)
        addView(ico)
        val title = TextView(context)
        title.setTextColor(Color.BLACK)
        title.textSize = 17f
        title.text = "专栏专题"
        title.typeface = Typeface.DEFAULT_BOLD
        val lp = LayoutParams(-2, -2)
        lp.marginStart = resources.getDimensionPixelSize(R.dimen.dimen5)
        lp.marginEnd = resources.getDimensionPixelSize(R.dimen.dimen2)
        addView(title, lp)
        recyclerView = RecyclerView(context)
        adapter = LiteAvHotAdapter(context)
        recyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
        recyclerView.adapter = adapter
        addView(recyclerView)

    }

    fun setCmsCatalogList(list: MutableList<CmsCatalog>) {
        adapter.setData(list)
    }


    class LiteAvHotAdapter(val context: Context) : RecyclerView.Adapter<LiteAvHotItemHolder>() {
        val data: MutableList<CmsCatalog> = ArrayList()
        fun setData(data: MutableList<CmsCatalog>?) {
            this.data.clear()
            data?.let {
                this.data.addAll(it)
            }
            notifyDataSetChanged()
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LiteAvHotItemHolder {
            return LiteAvHotItemHolder(LayoutInflater.from(context).inflate(R.layout.item_lite_av_hot_item, parent, false))
        }

        override fun onBindViewHolder(holder: LiteAvHotItemHolder, position: Int) {
            val d = data[position]
            holder.hotTitle.text = "#${d.name}"
            holder.itemView.setOnClickListener {
                val intent = Intent(context, LiteAvHotDetailActivity::class.java)
                intent.putExtra("CMS_CATALOG", d)
                context.startActivity(intent)
            }
        }

        override fun getItemCount(): Int {
            return data.size
        }

    }

    class LiteAvHotItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val hotTitle: TextView = itemView.findViewById(R.id.hotTitle)
    }


}